import { Component, EventEmitter, Output } from '@angular/core';
import { NotifyFieldUpdated, SiteNodeConfig } from '../../models';

@Component({
  selector: 'app-site-sidebar',
  templateUrl: './site-sidebar.component.html',
  styleUrl: './site-sidebar.component.scss'
})
export class SiteSidebarComponent {

  sidebarRightVisible: boolean = false;
  item?: SiteNodeConfig;
  @Output()
  onHide: EventEmitter<void> = new EventEmitter();

  onShow(item: SiteNodeConfig) {
    console.log("Site", "onShow");
    this.item = item;
    this.sidebarRightVisible = true;
  }
  onSidebarHide() {
    this.onHide.next();
    this.item = undefined;
  }

  notifyFieldUpdated(data: NotifyFieldUpdated) {
    if (data.TargetId == this.item?.Id) {
      for (const key in data.Values) {
        if (Object.prototype.hasOwnProperty.call(data.Values, key)) {
          const val = data.Values[key];
          const field = this.item?.Fields.find(f => f.Name == key);
          if (field)
            field.Value = val;
        }
      }
    }
  }
}
